# coding:utf-8
import requests
import xlwt
import xlrd
from lxml import etree


class PigData(object):
    def __init__(self):
        self.f = xlwt.Workbook(encoding='utf-8')
        self.sheet1 = self.f.add_sheet('任务列表',cell_overwrite_ok=True)
        self.rowsTitles =[u'编号',u'标题',u'简介',u'价格',u'截止时间',u'链接']
        for i in range(len(self.rowsTitles)):
            self.sheet1.write(0,i,self.rowsTitles[i],self.set_sty('Times new oman', 220, True))   # 写入第一行
        self.f.save('zbj.xls')

    def set_sty(self,str,height,bold=False):
        sty = xlwt.XFStyle()  # 初始化样式
        font = xlwt.Font()   # 创建字体
        font.name = str
        font.height = height
        font.colour_index = 2
        font.bold = bold
        sty.font = font
        return sty

    def getUrl(self):
        for i in range(33):
            url = 'http://task.zbj.com/t-ppsj/p{}s5.html'.format(i+1)
            self.spiderPage(url)


    def spiderPage(self,url):
        if url is None:
            return None
        try:
            data = xlrd.open_workbook('zbj.xls')
            table = data.sheets()[0]    # 通过索引获取
            rowcount = table.nrows      # 获取行数
            #proxies = {'http': 'http://221.202.248.52:80',}
            # user_agent = {}
            headers = {'User-Agent': 'Mozilla/5.0'}
            html_text = requests.get(url,headers = headers).text
        except Exception,e:
            print '网址错'
        finally:
            selector = etree.HTML(html_text)
            tds = selector.xpath('//*[@class="tab-switch tab-progress"]/table/tr')
            m=0
            for td in tds:
                data = []
                price = td.xpath('./td/p/em/text()')
                href = td.xpath('./td/p/a/@href')
                title = td.xpath('./td/p/a/text()')
                subTitle = td.xpath('./td/p/text()')
                deadline = td.xpath('./td/span/text()')

                price = price[0] if len(price)>0 else ''
                title = title[0] if len(title)>0 else ''
                href = href[0] if len(href)>0 else ''
                subTitle = subTitle[0] if len(subTitle)>0 else ''
                deadline = deadline[0] if len(deadline)>0 else ''

                data.append(rowcount + m)
                data.append(title)
                data.append(subTitle)
                data.append(price)
                data.append(deadline)
                data.append(href)

                for i in range(len(data)):
                    self.sheet1.write(rowcount+m,i,data[i])
                m+=1
                self.f.save('zbj.xls')

if '__main__':
    zbj = PigData()
    zbj.getUrl()
